/**
 * 分页查询方法
 * @param url
 * @param pageNo
 * @param pageSize
 */
function submit(url, pageNo, pageSize) {
    if(pageNo === undefined){
        pageNo = initPageNo;
    }
    if(pageSize === undefined){
        pageSize = initPageSize;
    }

    layui.use(['table', 'laypage','form'], function () {
        let table = layui.table;
        let laypage = layui.laypage;
        let requestSn = $('#requestSn').val();
        let templateNo = $('#templateNo').val();
        let channelNo = $('#job_channel').val();
        let signStatus = $('#signStatus').val();

        //第一个实例
        table.render({
            elem: '#searchTable'
            ,url: url //数据接口
            ,page: false //开启分页
            ,toolbar: '#tableToolbar'
            ,height: 'full-280'
            , method: 'POST'
            , headers: {"token": getCookie("token")}
            , contentType : "application/json"
            , where: {requestSn:requestSn,templateNo:templateNo,channelNo:channelNo,signStatus:signStatus,pageNo:pageNo,pageSize:pageSize}
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'requestSn', title: '流水号', width: '260', align: 'center'}
                , {field: 'templateNo', title: '模板编号', width: '180',align: 'center'}
                , {field: 'templateName', title: '模板名称', width: '200',align: 'center'}
                , {field: 'channelName', title: '渠道名称', width: '150',align: 'center'}
                , {field: 'sealType', title: '签章类型', width: '80',align: 'center'}
                , {field: 'createTime', title: '请求时间', width: '180',align: 'center',templet: function (d) {return toDateString(d.createTime);}}
                , {field: 'updateTime', title: '结束时间', width: '180', align: 'center',templet: function (d) {return toDateString(d.updateTime);}}
                , {field: 'differenceTime', title: '耗时', width: '80',align: 'center',templet: function (d) {return calculateTime(d.differenceTime);}}
                , {field: 'signStatusName', title: '处理状态', width: '100',align: 'center'}
                , {field: 'signResult', title: '处理结果', width: '150',align: 'center'}
                , {field: 'successCallCode', title: '回调状态',width: '100',align: 'center'}
                , {field: 'successCallMsg', title: '回调信息', width: '150',align: 'center'}
                , {field: 'callbackUrl', title: '业务系统回调地址', width: '350',align: 'center'}
                , {fixed: 'right', width: '150', title: '操作', align: 'center',toolbar: '#barSearch'} //添加工具条,
            ]]
            ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                if(res.code == 100){
                    errorMsg(res.msg);
                }else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.pageInfo.totalCount, //解析数据长度
                        "pageNo": res.data.pageInfo.pageNo, //解析数据长度
                        "pageSize": res.data.pageInfo.pageSize, //解析数据长度
                        "data": res.data.pageInfo.list //解析数据列表
                    };
                }
            }
            ,done:function (res) {
                //校验登陆状态
                checkLogin(res);
                //自定义分页
                laypage.render({
                    elem: 'tablePage'
                    , curr: res.pageNo
                    , count: res.count
                    , limit: res.pageSize
                    ,layout: ['page', 'limit','count', 'skip']
                    ,theme: '#1b2b34'
                    ,jump: function(obj,first){
                        if(!first){
                            submit(url, obj.curr, obj.limit);
                        }
                    }
                });
            }
        });

        //工具条
        table.on('tool(searchTable)', function(obj){
            let data = obj.data;
            if(obj.event === 'paramsDetail'){
                let id = data.id;
                let createTime = data.createTime;
                let signStatus = data.signStatus;
                xadmin.open("查看合同参数","./look.html?id="+id+"&&signStatus="+signStatus+"&&createTime="+createTime,500,650);
            }
            if(obj.event === 'reTry'){
                reTry(data);
            }
        });

        //头部按钮
        table.on('toolbar(searchTable)', function(obj){
            let checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'batch':
                    var data = checkStatus.data;
                    if(data.length == 0){
                        layer.msg('请选择要重签的数据', function(){
                        });
                    }else {
                        var count = 0;
                        for (var i = 0;i< data.length;i++){
                            var temData = data[i]
                            reTry(temData);
                            count++;
                        }

                        layer.msg('发生成功['+count+']条数据', {
                            icon: 1,
                            time: 3000,
                            end: function () {
                                // var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                // parent.layer.close(index);
                                $('#reloadData').trigger("click");
                            }
                        });
                    }
                    break;
            }
        });
    });
}

/**
 * 重签
 * @param data
 */
function reTry(data) {
    let url = serverPre + "/manager/scs/v2/signature/reTry";
    let url2 = serverPre + "/manager/scs/v2/requestLog/one?id="+data.refId+"&&date="+toDateString(data.createTime);
    let result = requestGet(url2);

    let commitData = JSON.stringify({
        "requestSn": data.requestSn,
        "channelNo": data.channelNo,
        "templateNo": data.templateNo,
        "createTime": toDateString(data.createTime),
        "callBackUrl": data.callbackUrl,
        "requestData":result.requestData,
        "paramsId":data.refId,
        "needSign": data.isSeal == '是' ? true : false,
    });
    $.ajax({
        url: url,
        data: commitData,
        type: 'POST',
        contentType: 'application/json',
        headers: {"token": getCookie("token")},
        success: function (res) {
            if(res.code == 0){
                layer.msg(res.msg, {
                    icon: 1,
                    time: 3000,
                    end: function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        // parent.layer.close(index);
                    }
                });
            }else {
                layer.msg(res.msg, {
                    icon: 2,
                    time: 5000,
                    end: function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        // parent.layer.close(index);
                    }
                });
            }
        },
        error: function (err) {
            errorMsg(err);
        }
    });
}


/**
 * 初始化渠道--所有可用渠道
 */
function queryAvailableChannels(form) {
    let url = serverPre + "/manager/scs/v2/channel/noAuth/list";
    let content = '<option value="">请选择</option>'; //全局变量
    let data = JSON.stringify({'pageNo':'1','pageSize':'100'});
    $.ajax({
        url: url,
        data: data,
        type: 'POST',
        contentType: 'application/json',
        headers: {'token': getCookie('token')},
        success: function (res) {
            if (res.code === 0) {
                let result = res.data.pageInfo.list;
                $.each(result,function (i,item) {
                    let channelNo = item.channelNo;
                    let channelName = item.channelName;
                    content += '<option value = "' + channelNo + '">' + channelName + '</option>'
                });
                $("#job_channel").html(content);
                form.render('select');
            }
        },
        error: function (err) {
            errorMsg(err);
        }
    });
}

/**
 * 提交表单
 * @param formData
 * @param url
 */
function submitForm(commitData, url) {
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form;
        //监听提交
        form.on('submit(submitBtn)', function (data) {
            debugger
            saveSealInfo(url, commitData);
        });
    })
}


/**
 * 隐藏添加按钮
 */
function hideAddButton(res) {
    var obj = res.data[0];
    if(obj.appId != null || obj.appId != undefined){
        $('#zjBtn').hide();
    }else {
        $('#fddBtn').hide();
    }
}

/**
 * 查看合同参数页面
 * @param result
 */
function buildOpenWin(result){
    if(result === undefined || result === null){
        errorMsg("返回数据为空");
    }
    if(result.channelName !== ""){
        $('#channelName').val(result.channelName);
    }
    if(result.templateNo !== ""){
        $('#templateNo').val(result.templateNo);
    }
    if(result.isSeal !== ""){
        $('#isSeal').val(result.isSeal);
    }
    if(result.callbackUrl !== ""){
        $('#callbackUrl').val(result.callbackUrl);
    }
    if(result.createTime !== ""){
        let time = toDateString(result.createTime);
        $('#createTime').val(time);
    }
    if(result.updateTime !== ""){
        let time = toDateString(result.updateTime);
        $('#updateTime').val(time);
    }
    if(result.differenceTime !== ""){
        $('#differenceTime').val(result.differenceTime);
    }
    if(result.requestData !== ""){
        $('#requestData').val(result.requestData);
    }
}
